#pragma once

#include "ConcurrentTcpConnection.h"


/// The top-level class of the HTTP multithread server
class MultithreadTcpServer
{
public:
	/// Constructs the server to listen to the specified port using the given io_service
	MultithreadTcpServer(std::size_t threadPoolSize, unsigned short port = 80);

	/// Run the server's io_service loop
	void Run();

private:
	/// Handles completion of an asynchronous accept operation
	void HandleAccept(const boost::system::error_code &error);

	/// Starts asynchronous accept operation
	void StartAccept();

	/// The io_service used to perform asynchronous operations.
	boost::asio::io_service _ioService;

	/// Acceptor used to listen for incoming connections
	boost::asio::ip::tcp::acceptor _acceptor;

	/// The next connection to be accepted.
	ConcurrentTcpConnection::Pointer _newConnection;

	/// The number of threads that will call io_service::run().
	std::size_t _threadPoolSize;
};
